<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;


use App\Logics\ChannelLogic;
use App\Models\ChannelModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Logics\SystermAdLogic;

//轮播图
class SystermAdController extends BaseController{

    private function getInstance(){
        static $ob=null;
        if($ob===null){
            $ob=new SystermAdLogic();
        }
        return $ob;
    }

    public function index(){
        $page=(int)request('page',1);
        $per_page = (int)request("per_page", 20);
        $title = trim(request('title'));
        $cha_id = (int)request('cha_id',0);
        $is_show_share = (int)request('is_show_share',-1);

        $para=[
            'page'=>$page,
            'per_page'=>$per_page,
            'title'=>$title,
            'cha_id'=>$cha_id,
            'is_show_share'=>$is_show_share
        ];
        $ob= $this->getInstance();
        $arr=$ob->index($para);
        if($arr && is_string($arr)) die(json_error($arr));


        $content= view("admin.systermAd.index",$arr) ;
        $data=[
            'content'=>$content,
            'header'=>'分类列表',
        ];
        return  $this->baseList($data);
    }


    //添加
    public function add(Request $request){
        if($_POST){
            $title=trim(request('title'));
            $content_url=trim(request('content_url'));
            $is_show_share=(int)request('is_show_share');
            $is_show_share==1?1:0;
            $cha_id=(int)request('cha_id');
            $para=[
                'title'=>$title,
                'content_url'=>$content_url,
                'is_show_share'=>$is_show_share,
                'cha_id'=>$cha_id
            ];
            $ob=$this->getInstance();
            $data=$ob->add($para,$request);
            $this->result($data);
        }

        $channel=ChannelLogic::pluck('cha_describe','cha_id');

        $content= view("admin.systermAd.add",['channel'=>$channel]) ;
        $data=[
            'content'=>$content,
            'header'=>'轮播图添加',
        ];
        return  $this->baseList($data);
    }

    //添加
    public function edit(Request $request){
        if($_POST){
            $title=trim(request('title'));
            $content_url=trim(request('content_url'));
            $is_show_share=(int)request('is_show_share');
            $is_show_share==1?1:0;
            $cha_id=(int)request('cha_id');
            $id=(int)request('id');
            $para=[
                'title'=>$title,
                'content_url'=>$content_url,
                'is_show_share'=>$is_show_share,
                'cha_id'=>$cha_id,
                'id'=>$id
            ];
            $ob=$this->getInstance();
            $data=$ob->edit($para,$request);
            $this->result($data);
        }

        $id=(int)request('id');
        $ob=$this->getInstance();
        $ad=$ob->getOne($id);
        if(empty($ad)) die(json_error('参数有误'));
        $channel=ChannelLogic::pluck('cha_describe','cha_id');

        $img_url=config('sys.img_url');
        $arr=['channel'=>$channel,'ad'=>$ad,'url'=>$img_url];


        $content= view("admin.systermAd.edit",$arr) ;
        $data=[
            'content'=>$content,
            'header'=>'轮播图编辑',
        ];
        return  $this->baseList($data);
    }

    //删除数据
    public function delete(){
        if($_POST){
            $id=(int)request('id');
            $ob=$this->getInstance();

            $data=$ob->delete($id);
            $this->result($data);
        }
    }


    //异步编辑一个字段
    public function ajaxEdit(){
        if($_POST){
            $id=(int)request('id');
            $ob=$this->getInstance();

            $data=$ob->ajaxEdit($id);
            $this->result($data);
        }
    }
















}